package com.neu.questionnaire_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neu.questionnaire_system.beans.ResponseEntity;
import com.neu.questionnaire_system.entity.Choice;
import com.neu.questionnaire_system.entity.Question;
import com.neu.questionnaire_system.entity.Questionnaire;
import com.neu.questionnaire_system.mapper.ClusterMapper;
import com.neu.questionnaire_system.mapper.QuestionMapper;
import com.neu.questionnaire_system.service.QuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.questionnaire_system.util.Constants;
import com.neu.questionnaire_system.util.UUIDGenerator;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xingle
 * @since 2022-11-07
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private ChoiceServiceImpl choiceService;

    public ResponseEntity insertQuestion(String qid, String questionType, String questionText) {
        ResponseEntity responseEntity = new ResponseEntity();
        String questionId = UUIDGenerator.getOneUUID();
        int result = questionMapper.insertQuestion(qid, questionId, questionType, questionText);
        responseEntity.setData(result);
        if (result > 0) {
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.ADD_MESSAGE);
        } else {
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.ADD_FAIL_MESSAGE);
        }
        return responseEntity;
    }

    //通过问卷id查询该问卷下的全部问题
    public ResponseEntity selectQuestionByQid(String qid) {
        ResponseEntity responseEntity = new ResponseEntity();
        List<Question> result = questionMapper.selectQuestionByQid(qid);
        responseEntity.setData(result);
        if (result != null) {
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.SELECT_MESSAGE);
        } else {
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.SELECT_FAIL_MESSAGE);
        }
        return responseEntity;
    }

    //根据问题id来对问题内容和类型进行更新
    public ResponseEntity updateQuestion(String questionId, String questionType, String questionText) {
        ResponseEntity responseEntity = new ResponseEntity();
        int result = questionMapper.updateQuestion(questionId, questionType, questionText);
        responseEntity.setData(result);
        if (result > 0) {
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.UPDATE_MESSAGE);
        } else {
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.UPDATE_FAIL_MESSAGE);
        }
        return responseEntity;
    }

    //根据问题id进行删除问题
    public ResponseEntity deleteQuestion(String questionId) {
        ResponseEntity responseEntity = new ResponseEntity();
        int result = questionMapper.deleteQuestion(questionId);
        responseEntity.setData(result);
        if (result > 0) {
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.DELETE_MESSAGE);
        } else {
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.DELETE_FAIL_MESSAGE);
        }
        return responseEntity;
    }

    //查询全部题目（题库）
    public ResponseEntity showAllQuestion() {
        ResponseEntity responseEntity = new ResponseEntity();
        List<Question> result = questionMapper.showAllQuestion();
        responseEntity.setData(result);
        if (result != null) {
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.SELECT_MESSAGE);
        } else {
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.SELECT_FAIL_MESSAGE);
        }
        return responseEntity;
    }

    //输入选项id并跳转到指定关联问题
    public ResponseEntity jumpToQuestion(String choiceId) {
        ResponseEntity responseEntity = new ResponseEntity();
        List<Question> result = questionMapper.jumpToQuestion(choiceId);
        responseEntity.setData(result);
        if (result != null) {
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.JUMP_MESSAGE);
        } else {
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.JUMP_FAIL_MESSAGE);
        }
        return responseEntity;
    }

    /**
     * created by libo 为创建的问卷批量插入题目
     *
     * @param questions 包含题目的集合
     * @param qid       问卷的qid
     */
    public void createQuestion(List<Question> questions, String qid) {
        for (Question q : questions
        ) {
            q.setQid(qid);

        }
        this.saveBatch(questions);
    }

    /**
     * created by libo 题目统计
     * question 题目的信息，键与数据库属性对应
     * answerChoice包含选择题每个选项的合计人数num，题号count和选项answerContent,占比percentage
     * questionBlock包含填空题填写的信息answerContent和题号count
     *
     * @param map 包含问卷的qid
     * @return 封装信息的统一的返回对象
     */
    public ResponseEntity quesStatistics(Map<String, Object> map) {
        ResponseEntity responseEntity = new ResponseEntity();
        Map<String, Object> res = new HashMap<>();
        List<Map<String, Object>> answerChoice = questionMapper.quesStatistics(map.get("qid").toString());
        List<Map<String, Object>> sum = questionMapper.queryEffectiveNum(map.get("qid").toString());
        for (Map<String, Object> m : sum
        ) {
            for (int i = 0; i < answerChoice.size(); i++) {
                if (Integer.parseInt(m.get("count").toString()) == (Integer.parseInt(answerChoice.get(i).get("count").toString()))) {
                    double d = Double.parseDouble(answerChoice.get(i).get("num").toString()) / Double.parseDouble(m.get("countSum").toString());
                    double d2 = d * 100;
                    String str = String.format("%.2f", d2);
                    answerChoice.get(i).put("percentage", str + "%");
                } else {
                    continue;
                }
            }
        }
        res.put("answerChoice", answerChoice);
        res.put("question", questionMapper.selectQuestionByQid(map.get("qid").toString()));
        res.put("questionBlock", questionMapper.queryBlock(map.get("qid").toString()));
        responseEntity.setData(res);
        responseEntity.setCode(Constants.SUCCESS_CODE);
        responseEntity.setMessage(Constants.SELECT_MESSAGE);
        return responseEntity;
    }

}
